package com.market.domain.transients;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.faces.context.FacesContext;

public enum DAY_OF_WEEK {
	SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
	private String label;

	private String getMessage(String key) {
		Locale locale = FacesContext.getCurrentInstance().getViewRoot()
				.getLocale();
		java.util.ResourceBundle resBundle = ResourceBundle.getBundle(
				"com.market.messages", locale);
		return resBundle.getString(key);
	}

	public Integer getValue() {
		return this.ordinal() + 1;
	}

	public String getLabel() {
		return this.getMessage("DAY_OF_WEEK_" + (this.getValue()));
	}
	
	
	public MarketDate getDate(Integer year, Integer week){
		Calendar now = Calendar.getInstance(java.util.Locale.CANADA);

		now.set(Calendar.YEAR, year);
		now.set(Calendar.WEEK_OF_YEAR,week);
		now.setFirstDayOfWeek(Calendar.THURSDAY);
		now.set(Calendar.DAY_OF_WEEK, this.getValue());
		return new MarketDate(now);
	}

	/**
	 * Return the specific day is not past and not present. For example, today
	 * is Tuesday, the comming date for Friday is the Friday of this week. But
	 * the comming date for Monday is the Monday of next week. Because Monday of
	 * this week is already pasted. The comming date of Tuesday is Tuesday of
	 * next week too.
	 * 
	 * @return
	 */
	public MarketDate getCommingDate() {
		Calendar now = Calendar.getInstance(java.util.Locale.CANADA);
		if (this.getValue() <= now.get(Calendar.DAY_OF_WEEK)) {
			if (now.get(Calendar.WEEK_OF_YEAR) == now
					.getMaximum(Calendar.WEEK_OF_YEAR)) {
				now.roll(Calendar.YEAR, true);
				now.set(Calendar.WEEK_OF_YEAR, 1);
			} else {
				now.roll(Calendar.WEEK_OF_YEAR, true);
			}
		}
		now.set(Calendar.DAY_OF_WEEK, this.getValue());
		return new MarketDate(now);
	}

	/**
	 * Return if this day is pasted, for example, Today is Tuesday, if this represent Monday that means has pasted,
	 * if this represent Wednesday that means has not pasted, if this represent Tuesday and current time is before 9:00 AM
	 * that means has not pasted yet, and if current time is after 9:00 AM that means has pasted. 
	 * @return
	 */
	
	public Boolean getIsPasted() {
		Calendar now = Calendar.getInstance(java.util.Locale.CANADA);
		if ((this.getValue() > now.get(Calendar.DAY_OF_WEEK))
				|| ((this.getValue() == now.get(Calendar.DAY_OF_WEEK))
						&& (now.get(Calendar.AM_PM) == Calendar.AM) && (now
						.get(Calendar.HOUR) < 9))) {
			return Boolean.FALSE;

		}
		return Boolean.TRUE;
	}
}